<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: xajax.product_info.php 278 2013-09-23 08:57:58Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 278 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-23 08:57:58 +0000 (Mon, 23 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

function getNewPrice($values) {
	global $db, $main;
	$has_attr_price = false;
	$has_staffel_price = false;

	$ajax = new xajaxResponse();
	$get = urldecode($values);
	$post = explode('&', $get);

	foreach($post as $posts) {
		list($key, $value) = explode('=', $posts);

		if(strstr($key, 'id['))
			$id[str_replace('id[','',str_replace(']','',$key))] = $value;

		if($key == 'products_qty')
			$qty = $value;

		else if($key == 'products_id')
			$pid = $value;
	}
	if($qty > 0) {
		$product = new product($pid);
		$price = new price($_SESSION['currency'], $_SESSION['customers_status']['customers_status_id']);
		
		$main_price = $price->GetPrice($pid, false, $qty, $product->data['products_tax_class_id']);
		
		$check_staffel = $price->GetGroupPrice($pid, $qty);
				
		if($check_staffel && $_SESSION['customers_status']['customers_status_graduated_prices'] == 1) {
			$staffel = ' <span class="product_staffel_price">('.SINGLE_PRICE.' '.$price->format($check_staffel, true, $product->data['products_tax_class_id']).')</span>';
			$has_staffel_price = true;
		}
		
		if(is_array($id)) {
			foreach($id AS $skey => $sval) {
				$attr_price = $db->db_query("SELECT
													pd.products_discount_allowed,
													pd.products_tax_class_id,
													p.options_values_price,
													p.price_prefix,
													p.options_values_weight,
													p.weight_prefix
												FROM
													".TABLE_PRODUCTS_ATTRIBUTES." p,
													".TABLE_PRODUCTS." pd
												WHERE
													p.products_id = '".$pid."'
												AND
													p.options_id = '".$skey."'
												AND
													pd.products_id = p.products_id
												AND
													p.options_values_id = '".$sval."'");
        			
				if($attr_price->fields['options_values_price'] != '0.0000')
					$has_attr_price = true;
					
				$val_price = $price->format($attr_price->fields['options_values_price'], false, $attr_price->fields['products_tax_class_id']);
				
				if($price->cStatus['customers_status_discount_attributes'] == 1 && $price->cStatus['customers_status_discount'] != 0.00) {
					$dicount = 0;
					$discount = $price->cStatus['customers_status_discount'];
					if($attr_price->fields['products_discount_allowed'] < $price->cStatus['customers_status_discount'])
						$discount = $attr_price->fields['products_discount_allowed'];
						
					$val_price = ($val_price - $val_price / 100 * $discount) * $qty;
				}
				else
				$val_price = $val_price * $qty;
				
				if ($attr_price->fields['price_prefix'] == '+') {
					$main_price += $val_price;
					$attribute_price += $val_price;
				} else {
					$main_price -= $val_price;
					$attribute_price -= $val_price;
				}
			}
		}	
		
		$old_price = ' <span class="product_ajax_price">('.trim($price->GetPrice($pid, true, $qty, $product->data['products_tax_class_id'])).')</span>';
		
		if($main_price <> ($main_price+$attribute_price)) {
			$main_price = $price->format($main_price, true).$old_price;
		} elseif(is_array($id) && !$has_attr_price && !$has_staffel_price && $qty > 1) {
			$main_price = $price->format($main_price, true).' <span class="product_ajax_price">('.FROM.trim($price->GetPrice($pid, true, 1, $product->data['products_tax_class_id'])).')</span>';
		} elseif(is_array($id) && !$has_attr_price && $has_staffel_price) {
			$main_price = $price->format($main_price, true).$old_price;
		} elseif(is_array($id) && $has_attr_price && $has_staffel_price) {
			$main_price = $price->format($main_price, true).$old_price;
		} elseif($has_staffel_price) {
			$min_price = $db->db_query("SELECT 
											MIN(personal_offer) as min_price 
										FROM 
											".TABLE_PERSONAL_OFFERS_BY.$price->actualGroup." 
										WHERE 
											products_id = '".$pid."' ");
			
			$possible_qty = $db->db_query("SELECT 
												MAX(quantity) AS qty 
											FROM 
												".TABLE_PERSONAL_OFFERS_BY.$price->actualGroup." 
											WHERE 
												products_id = '".$pid."' 
											AND 
												quantity <= '".$qty."'");
			
			$max_qty = $db->db_query("SELECT 
												MAX(quantity) AS qty 
											FROM 
												".TABLE_PERSONAL_OFFERS_BY.$price->actualGroup." 
											WHERE 
												products_id = '".$pid."'");
			
			$staffel_preis = $db->db_query("SELECT 
												personal_offer 
											FROM 
												".TABLE_PERSONAL_OFFERS_BY.$price->actualGroup." 
											WHERE 
												products_id = '".$pid."' 
											AND 
												quantity = '".$possible_qty->fields['qty']."'");
														
			$final_price = $price->AddTax($check_staffel*$qty, $price->TAX[$product->data['products_tax_class_id']]);
			$main_price = $price->format($final_price, true).' <span class="product_ajax_price">('.FROM.$price->format($price->AddTax($min_price->fields['min_price'], $price->TAX[$product->data['products_tax_class_id']]), true).' <span class="single_price">('.SINGLE_PRICE.($qty<$max_qty->fields['qty']?'bei '.$qty:FROM.$max_qty->fields['qty']).' Stk. = '.$price->format($price->AddTax($staffel_preis->fields['personal_offer'], $price->TAX[$product->data['products_tax_class_id']]), true).')</span>)</span>';
		} elseif($special_price = $price->CheckSpecial($pid)) {
			$main_price = $price->formatSpecial($pID, $price->AddTax(($special_price*$qty), $price->TAX[$product->data['products_tax_class_id']]), $price->AddTax($price->getPprice($pid), $price->TAX[$product->data['products_tax_class_id']]), true, 0, $qty);
		}elseif($qty > 1){
			$main_price = $price->format($main_price, true).' <span class="product_ajax_price">('.SINGLE_PRICE.trim($price->GetPrice($pid, true, 1, $product->data['products_tax_class_id'])).')</span>';
		}else
			$main_price = $price->format($main_price, true);

		$ajax->assign('product_info_price', 'innerHTML', $main_price);
	}
	return $ajax;
}

if(!is_object($xajax))
	$xajax = new xajax();

$xajax->registerFunction("getNewPrice");